//
//  TCLYSigninViewController.m
//  YuErmuYing
//
//  Created by LYang on 16/8/20.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "TCLYSigninViewController.h"
#import "KLSignInView.h"
#import "KLSICalendarView.h"
#import "KLSignInTool.h"
#import "KLSignInModel.h"
#import "Macros.h"
#import "TCLYMineModel.h"
#import "KLConst.h"
#import "TCLYUser.h"

@interface TCLYSigninViewController ()
@property (nonatomic, strong) KLSignInView *signInView;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) KLSICalendarView *calendarView;
@property (nonatomic, strong) NSMutableArray *signedArray;
@property (nonatomic, strong) TCLYMineModel * mineModel;
@property (nonatomic, strong) TCLYUser * user;
@end

@implementation TCLYSigninViewController

- (void)viewDidLoad {
    
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    
    self.navigationController.navigationBar.backgroundColor = statusColor;
    self.navigationController.navigationBar.barStyle = UIBarMetricsCompactPrompt;
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, screenWidth, 20)];
    view.backgroundColor = navColor;
    [self.navigationController.navigationBar addSubview:view];
    
    UILabel * titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"签到";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.frame = CGRectMake(0, 0, 50, 20);
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.navigationItem.titleView = titleLabel;
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(12, 22, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    leftBtn.tag = 91;
    [leftBtn addTarget:self action:@selector(statusBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 从xib加载视图
    NSArray *nibView = [[NSBundle mainBundle] loadNibNamed:@"KLSignInView" owner:nil options:nil];
    KLSignInView *signInView = [nibView firstObject];
    self.signInView = signInView;
    [self.scrollView addSubview:self.signInView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createWork) name:@"loadSigninSucceed" object:nil];
    self.mineModel = [[TCLYMineModel alloc] init];
    self.user = [[TCLYUser alloc] init];
    NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:self.user.userId, @"uid", nil];
    [self.mineModel loadSigninParameters:dic];
    [self loadCalendar];
    
    WEAKSELF
    self.signInView.signInBlock = ^ {
        @try {
            //设置已经签到的天数日期
            NSDate *currentDate = [NSDate date]; // 当前时间
            NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setTimeZone:GTMzone];
            [dateFormat setLocale:[NSLocale currentLocale]];
            [dateFormat setDateFormat:@"yyyy-MM-dd"];
            NSString *currentDateStr = [dateFormat stringFromDate:currentDate];
            int signedNum = [[currentDateStr substringFromIndex:8] intValue];
            
            [weakSelf.signedArray addObject:[NSNumber numberWithInt:signedNum]];
            weakSelf.calendarView.signArray = weakSelf.signedArray;
            weakSelf.calendarView.date = [NSDate date];
        } @catch (NSException *exception) {
            KLog(@"查看崩溃日志");
        } @finally {
            
        }
    };
    
    self.view.backgroundColor = [UIColor whiteColor];
}
-(void)createWork{
    NSMutableDictionary * data1 = [[NSMutableDictionary alloc] init];
    if ([self.mineModel.signinDic isEqual:@400]) {
        [data1 setValue:@[@{@"date":@"2016-08-01",@"sign":@"0"},@{@"date":@"2016-08-03",@"sign":@"0"}] forKey:@"monthSign"];
    }else{
        [data1 setValue:self.mineModel.signinArr forKey:@"monthSign"];
    }
//    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:self.mineModel.signinArr,@"monthSign", nil];
    @try {
        KLSignInModel * signInModel= [KLSignInModel dataWithSignInDict:data1];
        
        // 上部
        _signInView.signModel = signInModel;
        self.signedArray = [NSMutableArray arrayWithCapacity:31];
        // 日历
        KLMonthSign *monthSignModel = [[KLMonthSign alloc] init];
        for (int i = 0; i<signInModel.monthSignArray.count; i++) {
            monthSignModel = signInModel.monthSignArray[i];
            
            NSString *numberStr = [monthSignModel.day substringFromIndex:8];
            int signedNum = [numberStr intValue];
            
            if ([monthSignModel.sign isEqualToString:@"1"]) {
                [self.signedArray addObject:[NSNumber numberWithInt:signedNum]];
                //返回本月所有已签到的日期
                NSLog(@"%@",self.signedArray);
            }
        }
        //设置已经签到的天数日期
        self.calendarView.signArray = self.signedArray;
        self.calendarView.date = [NSDate date];
        
    } @catch (NSException *exception) {
        NSLog(@"查看崩溃日志");
    } @finally {
        
    }
}
- (void)loadCalendar {
    // 日历
    NSDate *currentDate = [NSDate date]; // 当前时间
    NSTimeZone* GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setTimeZone:GTMzone];
    [dateFormat setLocale:[NSLocale currentLocale]];
    [dateFormat setDateFormat:@"yyyy-MM-d"];
    NSInteger firstWeekday = [KLSignInTool firstWeekdayInThisMonth:currentDate];
    if (firstWeekday > 4) {
        self.calendarView.frame = CGRectMake(0, CGRectGetMaxY(self.signInView.frame), screenWidth, 414);
        [self.scrollView addSubview:self.calendarView];
    } else {
        self.calendarView.frame = CGRectMake(0, CGRectGetMaxY(self.signInView.frame), screenWidth, 356);
        [self.scrollView addSubview:self.calendarView];
    }
    
    
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
        [_scrollView setContentSize:CGSizeMake(screenWidth , screenHeight + 64 +50+20)];
        [_scrollView setContentInset:UIEdgeInsetsMake(0, 0, 1, 0)];
        _scrollView.showsVerticalScrollIndicator = NO;
        
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}
- (KLSICalendarView *)calendarView {
    if (!_calendarView) {
        _calendarView = [[KLSICalendarView alloc] init];
    }
    return _calendarView;
}

-(void)statusBtnAction:(id)sender{
    switch (((UIButton *) sender).tag) {
        case 91:{
            
            [self.navigationController popViewControllerAnimated:YES];
            if (self.isMineViewCtr) {
                self.navigationController.navigationBarHidden = YES;
                self.tabBarController.tabBar.hidden = NO;
            }else{
                self.navigationController.navigationBarHidden = NO;
            }
            
        }
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
